package com.nulgame.bukkit.plugin.cleaner;

import static org.mockito.Mockito.mock;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

import junit.framework.TestCase;

import org.bukkit.entity.Player;

import com.nulgame.bukkit.plugin.model.ChatInfo;

public class PlayerChatCleanerTestCase extends TestCase {
	public void testClean(){
		Set<Player> cleanedPlayer = new CopyOnWriteArraySet<Player>();
		Map<Player, Map<String, ChatInfo>> recentPlayerChatMap = new ConcurrentHashMap<Player, Map<String, ChatInfo>>() ;
		Long limitedTimeMillis = 1000L;
		
		Player player1 = mock(Player.class);
		Player player2 = mock(Player.class);
		Player player3 = mock(Player.class);
		Player player4 = mock(Player.class);
		
		cleanedPlayer.add(player1);
		cleanedPlayer.add(player2);
		cleanedPlayer.add(player3);
		cleanedPlayer.add(player4);
		
		Map<String, ChatInfo> playerChatMap1 = new ConcurrentHashMap<String, ChatInfo>();
		Map<String, ChatInfo> playerChatMap2 = new ConcurrentHashMap<String, ChatInfo>();
		Map<String, ChatInfo> playerChatMap3 = new ConcurrentHashMap<String, ChatInfo>();
		Map<String, ChatInfo> playerChatMap4 = new ConcurrentHashMap<String, ChatInfo>();
		
		recentPlayerChatMap.put(player1, playerChatMap1);
		recentPlayerChatMap.put(player2, playerChatMap2);
		recentPlayerChatMap.put(player3, playerChatMap3);
		recentPlayerChatMap.put(player4, playerChatMap4);
		
		long time = System.currentTimeMillis();
		
		playerChatMap1.put("player1 1", new ChatInfo(1L, time, time));
		playerChatMap1.put("player1 2", new ChatInfo(2L, time - 5, time - 5));
		//this chat will be wiped out
		playerChatMap1.put("player1 3", new ChatInfo(10L, time - 2000, time - 1000));
		
		//this chat will be wiped out
		playerChatMap2.put("player2 1", new ChatInfo(1L, time - 3000, time - 3000));
		//this chat will be wiped out
		playerChatMap2.put("player2 2", new ChatInfo(3L, time - 2000 , time - 900 ));
		playerChatMap2.put("player2 3", new ChatInfo(1L, time, time));
		
		playerChatMap3.put("player3 1", new ChatInfo(1L, time, time));
		playerChatMap3.put("player3 2", new ChatInfo(2L, time - 5, time - 5));
		//this chat will be wiped out
		playerChatMap3.put("player3 3", new ChatInfo(10L, time - 2000, time - 1000));
		
		//player4 will be wiped out
		playerChatMap4.put("player4 1", new ChatInfo(1L, time - 5000, time - 2000));
		playerChatMap4.put("player4 2", new ChatInfo(2L, time - 6000, time - 3000));
		playerChatMap4.put("player4 3", new ChatInfo(10L, time - 4000, time - 2000));
		
		PlayerChatCleaner cleaner = new PlayerChatCleaner(cleanedPlayer, recentPlayerChatMap, limitedTimeMillis);
		
		cleaner.clean();
		//verify for player1
		assertEquals(2, recentPlayerChatMap.get(player1).size());
		assertTrue(recentPlayerChatMap.get(player1).containsKey("player1 1"));
		assertEquals(1L, recentPlayerChatMap.get(player1).get("player1 1").total.longValue());
		assertEquals(time, recentPlayerChatMap.get(player1).get("player1 1").beginTimeMillis.longValue());
		assertEquals(time, recentPlayerChatMap.get(player1).get("player1 1").lastTimeMillis.longValue());
		
		assertTrue(recentPlayerChatMap.get(player1).containsKey("player1 2"));
		assertEquals(2L, recentPlayerChatMap.get(player1).get("player1 2").total.longValue());
		assertEquals(time - 5, recentPlayerChatMap.get(player1).get("player1 2").beginTimeMillis.longValue());
		assertEquals(time - 5, recentPlayerChatMap.get(player1).get("player1 2").lastTimeMillis.longValue());
		
		assertFalse(recentPlayerChatMap.get(player1).containsKey("player1 3"));
		
		//verify for  player2
		assertEquals(1, recentPlayerChatMap.get(player2).size());
		assertTrue(recentPlayerChatMap.get(player2).containsKey("player2 3"));
		assertEquals(time, recentPlayerChatMap.get(player2).get("player2 3").beginTimeMillis.longValue());
		assertEquals(time, recentPlayerChatMap.get(player2).get("player2 3").lastTimeMillis.longValue());
		
		
		//verify for player1
		assertEquals(2, recentPlayerChatMap.get(player3).size());
		assertTrue(recentPlayerChatMap.get(player3).containsKey("player3 1"));
		assertEquals(1L, recentPlayerChatMap.get(player3).get("player3 1").total.longValue());
		assertEquals(time, recentPlayerChatMap.get(player3).get("player3 1").beginTimeMillis.longValue());
		assertEquals(time, recentPlayerChatMap.get(player3).get("player3 1").lastTimeMillis.longValue());
		
		assertTrue(recentPlayerChatMap.get(player3).containsKey("player3 2"));
		assertEquals(2L, recentPlayerChatMap.get(player3).get("player3 2").total.longValue());
		assertEquals(time -5, recentPlayerChatMap.get(player3).get("player3 2").beginTimeMillis.longValue());
		assertEquals(time -5, recentPlayerChatMap.get(player3).get("player3 2").lastTimeMillis.longValue());
		
		assertFalse(cleanedPlayer.contains(player4));
		assertNull(recentPlayerChatMap.get(player4));
		
		
	}
}
